<template>
  <div class="article-container">
    <!-- 导航栏 -->
    <van-nav-bar title="文章详情" left-arrow @click-left="$router.go(-1)" />

    <!-- 文章详情 -->
    <div class="detail">
      <h3 class="title">{{article.title}}</h3>
      <div class="author">
        <van-image
          width="50"
          height="50"
          fit="cover"
          round
          :src="article.aut_photo"
        />
        <div class="text">
          <p class="name">{{article.aut_name}}</p>
          <p class="time">{{article.pubdate | fromNow}}</p>
        </div>
        <van-button round size="small" type="info" @click="followClick">{{article.is_followed?'取消关注':'+关注'}}</van-button>
      </div>
      <div class="content">
        <div v-html="article.content"></div>
      </div>
      <br />
      <br />
      <div class="live">
        <van-button
          :icon="article.attitude == 1?'good-job':'good-job-o'"
          hairline
          round
          size="small"
          plain
          type="primary"
          @click="likeClike"
          >{{article.attitude == 1?'取消点赞':'点赞'}}</van-button
        >&nbsp;&nbsp;&nbsp;&nbsp;
        <van-button 
         :icon="article.attitude == 0?'delete':'delete-o'"
        hairline round size="small" plain type="danger"
         @click="disLikeClike"
          >{{article.attitude == 0?'取消不喜欢':'不喜欢'}}</van-button
        >
      </div>
    </div>
  </div>
</template>
<script>
// 导入接口
import {getArticleInfo, addFllowing,removeFllowing,addLikings,removeLikings,addDisLike,removeDisLike} from './home.js'

// 导入moment
// import moment from "moment";
// // 设置moment语言
// moment.locale("zh-cn");
export default {
  name: 'myarticle',
  data () {
    return {
      article:{}
    }
  },
  methods: {
// 1.关注点击
async followClick(){
  if (this.article.is_followed) {
    let res = await removeFllowing({target:this.article.aut_id})
    // console.log(res);
    // 弹窗提示
    this.$toast.success('取消关注成功')
    // 修改当前关注状态
    this.article.is_followed = false
  } else {
    // 当前未关注，点击应该关注
     let res = await addFllowing({target:this.article.aut_id})
    // console.log(res);
    // 弹窗提示
    this.$toast.success('关注成功')
    // 修改当前关注状态
    this.article.is_followed = true
  }
},
// 2.点赞点击
async likeClike(){
  if (this.article.attitude == 1) {
    let res = await removeLikings({target:this.article.art_id})
    // console.log(res);
    // 弹窗提示
    this.$toast.success('取消点赞成功')
    // 修改当前点赞状态
    this.article.attitude = -1
  } else {
    // 当前未关注，点击应该关注
     let res = await addLikings({target:this.article.art_id})
    // console.log(res);
    // 弹窗提示
    this.$toast.success('点赞成功')
    // 修改当前关注状态
    this.article.attitude = 1
  }
},
// 3.不喜欢点击
async disLikeClike(){
  if (this.article.attitude == 0) {
    let res = await removeDisLike({target:this.article.art_id})
    // console.log(res);
    // 弹窗提示
    this.$toast.success('取消不喜欢成功')
    // 修改当前不喜欢状态
    this.article.attitude = -1
  } else {
    // 当前未不喜欢，点击应该不喜欢
     let res = await addDisLike({target:this.article.art_id})
    // console.log(res);
    // 弹窗提示
    this.$toast.success('不喜欢成功')
    // 修改当前不喜欢状态
    this.article.attitude = 0
  }
},

  },
  async created() {
    // 1.发送请求
    let res = await getArticleInfo({id:this.$route.query.id})
    console.log(res);
    // 2.绑定数据
    this.article = res.data.data
  },
    // filters: {
    //   fromNow(value) {
    //     return moment(value).fromNow();
    //   },
    // },
}
</script>

<style lang="less">
.article-container {
  .van-nav-bar{
    .van-nav-bar__content {
    background-color: #0099fa;
    .van-nav-bar__title {
      color: #fff;
    }
    .van-icon-arrow-left {
      color: #fff;
    }
  }
  }
  
  
  .detail {
  padding: 0px 10px;
  .title {
    font-size: 16px;
  }
  .live {
    text-align: center;
  }
  .author {
    padding: 10px 0;
    display: flex;
    align-items: center;
    .text {
      flex: 1;
      padding-left: 10px;
      line-height: 1.3;
      .name {
        font-size: 14px;
        margin: 0;
      }
      .time {
        margin: 0;
        font-size: 12px;
        color: #999;
      }
    }
  }
  .content {
    font-size: 14px;
    overflow: hidden;
    white-space: pre-wrap;
    word-break: break-all;
    img {
      max-width: 100%;
      background: #f9f9f9;
    }
  }
}
}
</style>
